<?php

/**
 * Script loader class
 *
 * @package OWL
 * @subpackage Core
 * @author trung
 * @since 1.5
 */
class Owl_Script {
    var $registered_script = array();
    var $queue = array();
    var $do = array();
    var $done = array();
    var $concat_ver = '';
    var $concat_str = '';
    var $html_str ='';
    var $do_concat = true;
    var $base_url="";
    var $direct_dir = array("ckeditor");
    
    function  __construct() {
        register_shutdown_function(array($this,"__destruct"));
        $this->Owl_Script();
    }
    function  __destruct() {
        return false;
    }
    function Owl_Script(){
        $this->base_url = SITE_URL;
    }
    function register_script($handle,$src,$deps=false,$ver=false){
        $script = array();
        if (empty($handle) || empty ($src))
            return false;
        if (isset ($this->registered_script[$handle]))
            return false;
        $script['src'] = $src;
        $script['ver'] = $ver ? $ver : '1.0';
        $script['deps'] = $deps;
        $this->registered_script[$handle]=$script;
        return true;
    }

    function _do_dependency($handles,$top = false){
        if ( !$handles = (array) $handles )
			return false;
        foreach($handles as $handle){
            if ( in_array($handle, $this->do, true) || in_array($handle, $this->done, true)) 
                continue ;
            $keep_going = true;
            if ( !isset($this->registered_script[$handle]) )
		$keep_going = false;
            elseif ( $this->registered_script[$handle]['deps'] && array_diff($this->registered_script[$handle]['deps'], array_keys($this->registered_script)) )
		$keep_going = false;
            elseif ( $this->registered_script[$handle]['deps'] && !$this->_do_dependency( $this->registered_script[$handle]['deps']) )
		$keep_going = false;
            if ( !$keep_going ) {
		if ( !$top )
			return false;
		else
			continue;
            }
            $this->do[] = $handle;
        }
        return true;
    }
    function print_footer_script(){
        $zip = $this->do_concat ? 1 : 0;
	if ( $zip && defined('ENFORCE_GZIP') && ENFORCE_GZIP )
		$zip = 'gzip';
        $this->_do_dependency($this->queue,true);
        $this->do_items();
        if (!empty($this->concat_str)){
            $ver = md5($this->concat_ver);
            $href = $this->base_url . "load-scripts.php?c={$zip}&load=" . trim($this->concat_str, ', ') . "&ver=$ver";
            echo "<script type='text/javascript' src='" . $href . "'></script>\n";
        }
        if ( !empty($this->html_str) )
            echo $this->html_str;
        $this->concat_str=$this->concat_ver=$this->html_str='';       
        return $this->done;
    }
    function do_items($handles=false){
        foreach( $this->do as $key => $handle ) {
			if ( !in_array($handle, $this->done) && isset($this->registered_script[$handle]) ) {
				if ( ! $this->registered_script[$handle]['src'] ) {
					$this->done[] = $handle;
					continue;
				}
				if ( $this->do_item( $handle) )
					$this->done[] = $handle;
				unset( $this->do[$key] );
			}
		}
    }
    function do_item($handle){
        $ver = $this->registered_script[$handle]['ver'];
        $src = $this->registered_script[$handle]['src'];
        if ( $this->do_concat ){
		if ( !$this->in_direct_dir($src)) {
				$this->concat_str .= "$handle,";
				$this->concat_ver .= "$handle.$ver";
				return true;
		}
	}
        $href = $this->_script_href( $src, $ver, $handle );
        $tag = "<script type='text/javascript' src='$href'></script>\n";
	if ( $this->do_concat )
		$this->html_str .= $tag;
	else
		echo $tag;
    }
   function unregister_style( $handles ) {
            $handle_list = explode(',', $handles);
            foreach ( (array) $handle_list as $handle ){
		unset($this->registered_script[$handle]);
            }
    }

    function enqueue( $handles ) {
                $handle_list = explode(',', $handles);               
                foreach ( (array) $handle_list as $handle ) {
                if ( !in_array($handle, $this->queue) && isset($this->registered_script[$handle]) ) {
                            $this->queue[] = $handle;
                        }
                }                
    }

    function dequeue( $handles ) {
	$handle_list = explode(',', $handles);
        foreach ( (array) $handle_list as $handle ) {
            unset( $this->queue[$handle] );
        }
    }
    function _script_href( $src, $ver, $handle ) {
		if ( !preg_match('|^https?://|', $src)  ) {
			$src = $this->base_url . $src;
		}
		//$src = add_query_arg('ver', $ver, $src);
		//return esc_url( $src );
                return  $src;
	}
    function in_direct_dir($src) {
		if ( !$this->direct_dir )
			return false;
		foreach ( (array) $this->direct_dir as $test ) {
			if ( false !== strpos($src, $test) )
				return true;
		}
		return false;
	}
}
// API
function register_script($handle,$src,$deps=false, $ver = false){
    global $script_control;
    return $script_control->register_script($handle,$src,$deps,$ver);
}
function enqueue_script( $handles ){
     global $script_control;
     return $script_control->enqueue($handles);
}
function dequeue_script( $handles ){
    global $script_control;
    return $script_control->dequeue($handles);
}
function unregister_script( $handles ) {
     global $script_control;
     return $script_control->unregister_script($handles);
}
function print_script(){
    global $script_control;
    return $script_control->print_footer_script();
}
